//==============================================================================
//
// Title:       ReadCalXmlFile.c
// Purpose:     A short description of the implementation.
//
// Created on:  2012-7-2 at 13:18:52 by luh.
// Copyright:   dafda. All Rights Reserved.
//
//==============================================================================

//==============================================================================
// Include files
#define hrChk(f) if (error = (f), FAILED (error)) goto Error; else


#include "RFBB.h"         
#include <utility.h>
#include <formatio.h> 
#include <ansi_c.h>
#include <cvixml.h>
#include <cvirte.h>
#include <userint.h>
#include <pathctrl.h>
#include <utility.h>
#include "ReadCalXmlFile.h"

//double dCalArray[24] = {0.0};

double dTXPWMCalData[12] = {0.0};
double dTXFBCalData[12] = {0.0};


void __decldllexp TX_TEST ReadCalXmlFile(tTestData *testData, tTestError *testError)
{

    ErrMsg errMsg = {'\0'};
    ERRORINFO errorInfo;   
    ErrMsg szLogMsg;
    CAObjHandle hStepName = 0;
    char* pszStepName = NULL;
    char szStepName[BUFFER_SIZE_256] ={'\0'};
    char kszShmonAgent[BUFFER_SIZE_256] ={'\0'};
    double dTestNum = 0.0;
    int  error = 0;
    int  iPassFail = 0;
    int i = 0;
    int iPipeNum = 0;
    int iReturn = 0;
     char szOutputMessage[BUFFER_SIZE_256] = {'\0'};


    ListType aRouteElementList = 0;
    ListType  aFreqElementList =0;
    CVIXMLDocument aDocument = 0;
    CVIXMLElement aRootElement = 0;
    CVIXMLElement aLineElement = 0;
    CVIXMLElement aParentElement = 0;
    CVIXMLElement aChildElement = 0;
    CVIXMLElement aCurElement = 0;

    CVIXMLElement aRouteElement = 0;
    CVIXMLElement aFreqElement = 0;
    CVIXMLElement aCalElement = 0;

    CVIXMLAttribute aAttribute = 0;
    CVIXMLAttribute aCalAttribute = 0;

    char* pszCalFilePath = NULL;  //"C:\\SWIFT\\Sources\\Readxml\\FRIG_BB_CAL.xml"
    char szAuthorName[64] = {'\0'};
    char szFrequency[64] = {'\0'};
    char szCalvalue[64] = {'\0'};
    char szValue[64] = {'\0'};

    int iRootIndex = 1;
    int iRouteIndex  = 0;
    int iFreqIndex = 0;
    char szDate[64] = {'\0'};
    int iCalDate = 0;
    int iCurDate = 0;
    int iDateOffSet = 0;
    int iNumOfChildren = 0;
    int iNumOfFrequency = 0;
    int iArrayNum = 0;
    float dCalvalue = 0.0; 

    char* pszDate; // Do not free this string.
    char* pszTime; // Do not free this string.
    char pszStamp[64] = {'\0'};     
    //////////////////////////////////////////////////////////////////////////   
    // Resolve StepName 
    tsErrChk(TS_SeqContextGetProperty (testData->seqContextCVI, &errorInfo, 
                                       TS_SeqContextStep, CAVT_OBJHANDLE, 
                                       &hStepName));
    tsErrChk(TS_StepGetProperty (hStepName, &errorInfo, TS_StepName, CAVT_CSTRING, 
                                 &pszStepName));

    sprintf( szLogMsg, TEST_NAME_EVENT_FMTSTR, pszStepName );
    ErrChk( EELog_INEventLogLogEvent( g_hEventLog, NULL, szLogMsg ) );

    // Post message to GUI
    tsErrChk(TS_ThreadPostUIMessageEx (g_hThreadPostUIMessage, NULL,
                                       TS_UIMsg_ProgressText, 0.0,
                                       pszStepName, 0, VTRUE));

    //--------------------------------------------------------------------
    // Get Test Parameters from Sequence FileGlobals    
    sprintf(szStepName, "%s.PARAMETERS.FRIG_CAL_FILE_PATH", TESTPLAN_BASE_PATH_EXT );    
    tsErrChk(TS_PropertyGetValString(testData->seqContextCVI, &errorInfo, szStepName, 0, &pszCalFilePath));
    //--------------------------------------------------------------------
    sprintf( szLogMsg, "##### Read RF cable calibration file #####");
    ErrChk(EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg));
       
    
    // Load XML document
    hrChk(CVIXMLLoadDocument(pszCalFilePath, &aDocument));
    CVIXMLGetRootElement(aDocument, &aRootElement );    
    
    CVIXMLGetChildElementByTag (aRootElement, "HEADER", &aLineElement);
    CVIXMLGetChildElementByTag (aLineElement, "AUTHOR", &aCurElement);
    CVIXMLGetElementValue (aCurElement, szAuthorName); 
    
    CVIXMLGetChildElementByTag (aLineElement, "DATE", &aCurElement);
    CVIXMLGetElementValue (aCurElement, szDate); 
    
    iCalDate = atoi(szDate);   
    
    
    pszDate = DateStr(); 
    
    // Format date
    strncpy(pszStamp, pszDate+6, 4);
    strncat(pszStamp, pszDate, 2);
    strncat(pszStamp, pszDate+3, 2);  
    
    iCurDate = atoi(pszStamp);
    
    iDateOffSet = iCurDate  - iCalDate;
    
    if(iDateOffSet < (-1))
    {
         MessagePopup("Pay Attention","Calibration File is outdated !");
         error = -999;
         CVIXMLDiscardElement(aCurElement); 
         CVIXMLDiscardElement(aLineElement);
         goto Error;
    }
    
    CVIXMLDiscardElement(aCurElement); 
    CVIXMLDiscardElement(aLineElement);
    
    CVIXMLFindElements( aRootElement, "ROUTE", &aRouteElementList);
    iNumOfChildren = ListNumItems( aRouteElementList );
    
    
    for( iRouteIndex = 1; iRouteIndex < (iNumOfChildren + iRootIndex); iRouteIndex++ ) 
    {
        
        CVIXMLGetChildElementByIndex(aRootElement,iRouteIndex,&aChildElement);
        CVIXMLGetAttributeByIndex(aChildElement,0,&aAttribute);
        CVIXMLGetAttributeValue(aAttribute,szFrequency); 
    
        CVIXMLFindElements( aChildElement, "FREQUENCY", &aFreqElementList);
        iNumOfFrequency = ListNumItems( aFreqElementList );        
        for( iFreqIndex = 0; iFreqIndex < iNumOfFrequency; iFreqIndex++ ) 
        {            
            CVIXMLGetChildElementByIndex(aChildElement,iFreqIndex,&aFreqElement);
            CVIXMLGetAttributeByIndex(aFreqElement,0,&aCalAttribute);
            CVIXMLGetAttributeValue(aCalAttribute,szCalvalue);
          
            CVIXMLGetChildElementByTag( aFreqElement, "CALVALUE", &aCalElement);
            CVIXMLGetElementValue( aCalElement, szCalvalue );
            dCalvalue = atof(szCalvalue);

            if(iRouteIndex <= 4)
            {
                iArrayNum = ((iRouteIndex - 1) * 3) + iFreqIndex;
                dTXPWMCalData[iArrayNum] = dCalvalue;
            }
            else
            {
                iArrayNum = ((iRouteIndex - 1) * 3) + iFreqIndex - 12;
                dTXFBCalData[iArrayNum]  = dCalvalue;
            }
        }
    }
    
    CVIXMLDiscardElement(aCalElement); 
    CVIXMLDiscardElement(aFreqElement);
    CVIXMLDiscardElement(aChildElement);
    CVIXMLDiscardElement(aRootElement);

Error:
    
#if(ON_OFF != SIMU_VERS) 
    
    if(iPassFail == 1)
    {
        testData->result = PASS;
    }
    else 
    {
        testData->result = FAIL;
    }


    // FREE RESOURCES
    CA_DISCARD_OBJ( hStepName );
    CA_FREE( pszStepName );

    // If an error occurred, set the error flag to cause a run-time error in TestStand.
    if(error < 0)
    {
        testError->errorFlag = TRUE;
        testError->errorCode = error;
        testData->replaceStringFuncPtr(&testError->errorMessage, errMsg);
    }
#else
    testData->result = PASS;
#endif

   // Clear message from GUI
   TS_ThreadPostUIMessageEx (g_hThreadPostUIMessage, NULL,
                                 TS_UIMsg_ProgressText, 0.0, " ", 0, VTRUE);

   return;    
}
